package com.rr.middle;

/**
 * @author zhuangcanrun
 * @Date 2021/12/13/22:34
 * @description
 **/
/*
* 保持城市天际线
* https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/
* */
public class MaxIncreaseKeepingSkyline {
    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int xLen = grid[0].length;
        int yLen = grid.length;
        int[] x = new int[xLen];
        int[] y = new int[yLen];
        for (int i = 0; i < yLen; i++) {
            for (int j = 0; j < xLen; j++) {
                y[i] = Math.max(y[i], grid[i][j]);
                x[j] = Math.max(x[j], grid[i][j]);
            }
        }
        int ans = 0;
        for (int i = 0; i < yLen; i++) {
            for (int j = 0; j < xLen; j++) {
                ans += Math.min(y[i], x[j]) - grid[i][j];
            }
        }
        return ans;
    }
}
